<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;

class AuthMiddleware
{
    /**
     * Handle an incoming request.
     * @param Request $request
     * @param Closure(Request): (Response|RedirectResponse) $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next): mixed
    {
        $loginUser = Auth::guard('api')->user();

        if (!$loginUser){
            return response()->json(['code'=>401,'message'=>'未授权！']);
        }
        if ($loginUser['deleted'] === 1){
            return response()->json(['code'=>401,'message'=>'账户已禁用！']);
        }

        $request->attributes->add(['userInfo'=>$loginUser]);

        return $next($request);
    }
}
